package com.wzxy.wzinfo.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import com.wzxy.wzinfo.service.IMyJpaService;
import com.wzxy.wzinfo.utils.QueryParamUtil;

@Lazy(value=true)
public class MyJpaService<T,ID extends Serializable> implements IMyJpaService<T,ID> {

	private JpaRepository<T, ID> jpaRepository;
	private JpaSpecificationExecutor<T> jpaSpecificationExecutor;

	public void setJpaRepository(JpaRepository<T, ID> jpaRepository) {
		this.jpaRepository = jpaRepository;
	}
	public void setJpaSpecificationExecutor(JpaSpecificationExecutor<T> jpaSpecificationExecutor) {
		this.jpaSpecificationExecutor = jpaSpecificationExecutor;
	}


	@Override
	public T save(T t) {
		return jpaRepository.save(t);
	}

	@Override
	public T findOne(ID id) {
		
		return jpaRepository.findOne(id);
	}

	@Override
	public boolean exists(ID id) {
		// TODO Auto-generated method stub
		return jpaRepository.exists(id);
	}

	@Override
	public long count() {
		// TODO Auto-generated method stub
		return jpaRepository.count();
	}

	@Override
	public void delete(ID id) {
		jpaRepository.delete(id);
	}

	@Override
	public void delete(T entity) {
		jpaRepository.delete(entity);
	}

	@Override
	public List<T> findAll() {
		// TODO Auto-generated method stub
		return jpaRepository.findAll();
	}

	@Override
	public List<T> findAll(Sort sort) {
		// TODO Auto-generated method stub
		return jpaRepository.findAll(sort);
	}

	@Override
	public List<T> findAll(Iterable<ID> ids) {
		// TODO Auto-generated method stub
		return jpaRepository.findAll(ids);
	}

	@Override
	public void deleteInBatch(Iterable<T> entities) {
		jpaRepository.delete(entities);
		
	}

	@Override
	public void deleteAllInBatch() {
		jpaRepository.deleteAllInBatch();
	}

	@Override
	public T getOne(ID id) {
		// TODO Auto-generated method stub
		return jpaRepository.getOne(id);
	}

	@Override
	public Page<T> findAll(Pageable pageable) {
		// TODO Auto-generated method stub
		return jpaRepository.findAll(pageable);
	}
	
	
	/*------------jpaSpecificationExecutor(JPA规范执行-可动态拼接参数)----------*/

	@Override
	public T findOne(Specification<T> spec) {
		// TODO Auto-generated method stub
		return jpaSpecificationExecutor.findOne(spec);
	}

	@Override
	public List<T> findAll(Specification<T> spec) {
		// TODO Auto-generated method stub
		return jpaSpecificationExecutor.findAll(spec);
	}

	@Override
	public Page<T> findAll(Specification<T> spec, Pageable pageable) {
		// TODO Auto-generated method stub
		return jpaSpecificationExecutor.findAll(spec, pageable);
	}

	@Override
	public List<T> findAll(Specification<T> spec, Sort sort) {
		// TODO Auto-generated method stub
		return jpaSpecificationExecutor.findAll(spec, sort);
	}

	@Override
	public long count(Specification<T> spec) {
		// TODO Auto-generated method stub
		return jpaSpecificationExecutor.count(spec);
	}
	@Override
	public T update(T t,ID id) {
		T t1 = findOne(id);
		t1 = QueryParamUtil.updateParam(t, t1);
		return save(t1);
	}

}
